package capstoneproject.dudiachi.Controller;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import capstoneproject.dudiachi.DataAccess.DBHelper;
import capstoneproject.dudiachi.Model.Festival;
import capstoneproject.dudiachi.Model.Province;
import capstoneproject.dudiachi.Model.TagType;

public class FestivalManager {
	private ArrayList<Festival> listFestival;
	private DBHelper helper;
	private ProvinceManager pm;
	/**
	 * <pre>
	 * タッグ�?�データ列�?�行�?�インデックスを�?��?��?��?�。
	 * </pre>
	 */
	private List<TagType> listTag;

	/**
	 * <pre>
	 * コンストラクタ。 
	 * 祭りリストを�?��?��?�をロード。
	 * </pre>
	 * 
	 * @param ctx
	 */
	public FestivalManager(Context ctx) {
		super();
		helper = new DBHelper(ctx);
		listFestival = new ArrayList<Festival>();
		listTag = new ArrayList<TagType>();
		loadListFestival();
		pm = new ProvinceManager(ctx);
	}

	/**
	 * Festivalテーブル�?�らデータを�?��?��?��?{@link Festival}型�?��?列リスト�?�入れ�?��?�。
	 * 
	 * @throws SQLException
	 */
	private void loadListFestival() throws SQLException {
		helper.open();
		String[] columns = { Festival.COLUMN0, Festival.COLUMN1,
				Festival.COLUMN2, Festival.COLUMN3, Festival.COLUMN4,
				Festival.COLUMN5, Festival.COLUMN6, Festival.COLUMN7,
				Festival.COLUMN8, Festival.COLUMN9, Festival.COLUMN10,
				Festival.COLUMN11, Festival.COLUMN12, Festival.COLUMN13,
				Festival.COLUMN14, Festival.COLUMN15, Festival.COLUMN16,
				Festival.COLUMN17, Festival.COLUMN18, Festival.COLUMN19,
				Festival.COLUMN20 };
		Cursor c = helper.getData(Festival.TABLE_NAME, columns, null, null,
				null); // con tro chi den dong' output cua
		// ham
		// query()
		c.moveToFirst(); // chuyen den dong dau tien
		// get cac dong ra va them vao arraylist

		while (c.isAfterLast() == false) {
			Festival objFestival = new Festival(c.getString(0), c.getString(1),
					c.getString(2), c.getString(3), c.getString(4),
					c.getString(5), c.getString(6), c.getString(7),
					c.getString(8), c.getString(9), c.getString(10),
					c.getString(11), c.getString(12), c.getInt(13),
					c.getInt(14), c.getInt(15), c.getInt(16), c.getInt(17),
					c.getInt(18), c.getString(19), c.getString(20));
			listFestival.add(objFestival);
			TagType wtf = new TagType(c.getPosition(), c.getString(20));
			listTag.add(wtf);
			c.moveToNext();
		}
		helper.close();
	}

	/**
	 * タッグ�?�探�?��?��?�。
	 * 
	 * @param tag
	 *            　ストリング型�?�入力
	 * @return　{@link Festival}型�?��?列リスト型�?�変数
	 */
	@SuppressLint("DefaultLocale")
	public ArrayList<Festival> searchByTag(String tag) {
		ArrayList<Festival> result = new ArrayList<Festival>();
		for (TagType t : listTag) {
			if (t.getTag().toLowerCase().contains(tag.toLowerCase())) {
				result.add(listFestival.get(t.getPosition()));
			}
		}
		return result;
	}

	/**
	 * ゲッター
	 * 
	 * @return
	 */
	public ArrayList<Festival> getListFestival() {

		return listFestival;
	}

	/**
	 * デート�?�祭りを探�?��?��?�。
	 * 
	 * @param date
	 *            　日
	 * @param month
	 *            　月
	 * @return　{@link Festival}型�?��?列リスト型
	 */
	public ArrayList<Festival> searchByDate(int date, int month, int year) {
		ArrayList<Festival> result = new ArrayList<Festival>();
		ConvertBetweenSonarLunar wtf = new ConvertBetweenSonarLunar();
		for (Festival f : listFestival) {
			if (((f.getFestivalDayBegin() == date))
					&& (f.getFestivalMonthBegin() == month)
					&& (f.getIsLunar() != 1)) {
				result.add(f);
			}
		}
		for (Festival f : listFestival) {
			int lunar[] = wtf.Solar2Lunar(date, month, year);

			if (((f.getFestivalDayBegin() == lunar[0]))
					&& (f.getFestivalMonthBegin() == lunar[1])
					&& (f.getIsLunar() == 1)) {
				result.add(f);
			}
		}
		return result;
	}

	/**
	 * 「ProvinceID]�?�探�?��?��?�。
	 * 
	 * @return　「Province�?型�?��?列
	 */
	public ArrayList<Festival> searchByProvince(String province) {
		ArrayList<Festival> result = new ArrayList<Festival>();
		for (Festival od : listFestival) {
			if (od.getProvince().equalsIgnoreCase(province)) {
				result.add(od);
			}
		}
		return result;
	}

	public Province getFKOldProvince(String id) {
		return pm.searchByID(id);
	}

	/**
	 * 「id]�?�探�?��?��?�。
	 * 
	 * @return　１�?�「Festival�?
	 */
	public Festival searchByID(String id) {
		for (Festival op : listFestival) {
			if (op.getFestivalID().equalsIgnoreCase(id)) {
				return op;
			}
		}
		return null;
	}
}
